package com.xdtech.base.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.base.dao.ProvinceDao;
import com.xdtech.base.model.Province;
import com.xdtech.base.service.ProvinceService;
import com.xdtech.base.vo.ProvinceItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-06-02 14:00:50
 * @since 1.0
 * @see
 */
@Service
public class ProvinceServiceImpl implements ProvinceService {
	private Log log = LogFactory.getLog(ProvinceServiceImpl.class);
	@Autowired
	private ProvinceDao provinceDao;
	@Autowired
	private BaseDao baseDao;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:00:50
	 * @modified by
	 * @param entity
	 */
	public void save(Province entity) {
		provinceDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:00:50
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<Province> entities) {
		provinceDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:00:50
	 * @modified by
	 * @param entity
	 */
	public void delete(Province entity) {
		provinceDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:00:50
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		provinceDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:00:50
	 * @modified by
	 * @param id
	 * @return
	 */
	public Province get(Long id) {
		return provinceDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:00:50
	 * @modified by
	 * @return
	 */
	public List<Province> getAll() {
		return provinceDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:00:50
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> provinces = null;
		long rows = 0;
		if (pg!=null) {
			Page<Province> page = provinceDao.findPage(pg,"from Province order by createTime desc", values);
			provinces = BeanUtils.copyListProperties(
					ProvinceItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<Province> provinceList = provinceDao.find("from Province order by id desc", values);
			provinces = BeanUtils.copyListProperties(
					ProvinceItem.class, provinceList);
			rows = provinces.size();
		}
		results.put("total", rows);
		results.put("rows", provinces);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:00:50
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateProvince(ProvinceItem item) {
		Province province = null;
		if (item.getId()==null) {
			province = new Province();
		}else {
			province = provinceDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(province, item);
		provinceDao.save(province);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:00:50
	 * @modified by
	 * @param provinceId
	 * @return
	 */
	public ProvinceItem loadProvinceItem(Long provinceId) {
		Province province = provinceDao.get(provinceId);
		ProvinceItem provinceItem = new ProvinceItem();
		BeanUtils.copyProperties(provinceItem, province);
		return provinceItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:00:50
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteProvinceInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:00:50
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteProvinceInfo(List<Long> provinceIds) {
		for (Long id : provinceIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:00:50
	 * @modified by
	 * @return
	 */
	@Override
	public List<ProvinceItem> loadItems() {
		List<Province> provinces = getAll();
		return ProvinceItem.createItems(provinces);
	}

}
